/*
 * Copyright 2009 Eat Local Food, LLC
 * Copyright (c) 2007 osCommerce (this file was written after
 * code review of osCommerce).
 *
 * This file is part of gwtCommerce.
 *
 * gwtCommerce is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 2 of the License, or
 * (at your option) any later version.
 *
 * gwtCommerce is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with gwtCommerce.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.elf.gwt.admin.client.components;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import com.elf.gwt.admin.client.AdminComponentController;
import com.elf.gwt.admin.client.utils.AdminUtils;
import com.elf.gwt.common.client.model.ManufacturerMetaModel;
import com.elf.gwt.common.client.model.ManufacturerModel;
import com.google.gwt.json.client.JSONArray;
import com.google.gwt.json.client.JSONObject;
import com.google.gwt.user.client.ui.CheckBox;
import com.google.gwt.user.client.ui.FileUpload;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Hidden;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.TextBox;

/**
 *  
 */
public class ManufacturersAdministrativeComponent extends EditTableComponent
{	
	protected ManufacturerMetaModel manufacturerMetaModel;
	protected ManufacturerModel manufacturerModel;
	protected ManufacturerModel emptyManufacturerModel;
	protected List<ManufacturerModel> manufacturerModels;

	/**
	 * Constructor
	 */
	public ManufacturersAdministrativeComponent(AdminComponentController controller, String stylePrefix)
	{
		super(controller, stylePrefix);
		manufacturerModel = new ManufacturerModel();
		emptyManufacturerModel = new ManufacturerModel();
		manufacturerModels = new ArrayList<ManufacturerModel>();
		tableType = "manufacturers";
	}

	public boolean updateFromHistoryParameters(String[] params)
	{
		int id = 0;
		if (params.length > 1) id = Integer.parseInt(params[1]);
		int sz = manufacturerModels.size();
		for (int i = 0; i < sz; i++)
		{
			ManufacturerModel model = manufacturerModels.get(i);
			if (model.getId() == id)
			{
				manufacturerModel.update(model);
			}
		}
		return super.updateFromHistoryParameters(params);
	}

	protected void buildMetaModel(JSONObject jsonObject) throws Exception
	{
		manufacturerMetaModel = new ManufacturerMetaModel(jsonObject);
		super.buildMetaModel(jsonObject);
	}

	protected void buildModel(JSONArray jsonArray) throws Exception
	{
		manufacturerModels.clear();
		super.buildModel(jsonArray);
	}

	protected void buildModel(JSONObject jsonObject) throws Exception
	{
		ManufacturerModel model = new ManufacturerModel(jsonObject);
		model.setIndex(manufacturerModels.size());
		manufacturerModels.add(model);
	}

	public ManufacturerModel getManufacturerModel(int index) {
		return manufacturerModels.get(index);
	}

	public ManufacturerModel getManufacturerModelById(int id) {
		ManufacturerModel manufacturerModel = null;
		for (Iterator<ManufacturerModel> iter = manufacturerModels.iterator(); iter.hasNext(); )
		{
			ManufacturerModel model = iter.next();
			if (model.getId() == id) {
				manufacturerModel = model;
				break;
			}
		}
		return manufacturerModel;
	}

	public List<ManufacturerModel> getManufacturerModels() {
		return manufacturerModels;
	}

	public ManufacturerModel getManufacturerModel() {
		return manufacturerModel;
	}

	public void buildFromModel() throws Exception
	{
		numberOfTableEntries = manufacturerModels.size();
		if (edit)
			numberOfTableColumns = 4;
		else 
			numberOfTableColumns = 3;
		extraRows = 2;
		super.buildFromModel();
	}

	public void displayHeader(Grid grid) {
		grid.setText(0, 0, "Name");
		grid.setText(0, 1, "Image");
		grid.setText(0, 2, "URL");
		if (edit) grid.setText(0, 3, "Mark for Deletion");
	}

	public void displayRow(Grid grid, int row, int entryIndex) {
		displayRow(grid, row, manufacturerModels.get(entryIndex));
	}

	public void displayEmptyRow(Grid grid, int row) {
		displayRow(grid, row, emptyManufacturerModel);
	}
	
	public void displayRow(Grid grid, int row, ManufacturerModel model) 
	{
		Grid imageGrid = new Grid(2,1);
		imageGrid.setText(1, 0, model.getImage());
		grid.setWidget(row, 1, imageGrid);
		
		if (edit) {
			Hidden idHidden = new Hidden();
			idHidden.setName("manufacturerId");
			idHidden.setValue(String.valueOf(model.getId()));
			elements.put("manufacturerId-" + row, idHidden);

			TextBox nameTextBox = new TextBox();
			nameTextBox.setWidth("160px");
			if (model.getId() > 0)
				nameTextBox.setText(model.getName());
			elements.put("manufacturerName-" + row, nameTextBox);
			grid.setWidget(row, 0, nameTextBox);

			Date now = new Date();
			long intNow = now.getTime();
			FileUpload upload = new FileUpload();
			upload.setName("manufacturerImage-" + intNow + "-" + row);
			elements.put("manufacturerImage-"+row, upload);
			imageGrid.setWidget(0, 0, upload);
			
			TextBox urlTextBox = new TextBox();
			urlTextBox.setWidth("300px");
			urlTextBox.setText(model.getUrl());
			elements.put("manufacturerUrl-" + row, urlTextBox);
			grid.setWidget(row, 2, urlTextBox);

			if (model.getId() > 0) {
				CheckBox deleteCheckBox = new CheckBox();
				elements.put("manufacturerDelete-" + row, deleteCheckBox);
				grid.setWidget(row, 3, deleteCheckBox);
			}
		}
		else {
			grid.setText(row, 0, model.getName());
			
			Image image = new Image();
			String imageUrl = AdminUtils.getImageManufacturersDirectory() + model.getImage();
			image.setUrl(imageUrl);
			image.setTitle(model.getName());
			//String[] imageSize = categoryModel.getImageSize();
			//image.setSize(imageSize[0], imageSize[1]);
			imageGrid.setWidget(0, 0, image);

			grid.setText(row, 2, model.getUrl());
		}
	}

}
